package com.fatesgo.controller;

import com.fatesgo.mapper.StudentMapper;
import com.fatesgo.pojo.Student;
import com.fatesgo.util.Response;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private HttpServletRequest request;
    @Autowired(required = false)
    private StudentMapper studentMapper;

    @GetMapping("/getStudentInfo/{id}")
    public Response getStudentInfoById(@PathVariable("id") int id) {
        Student student = studentMapper.getStudentInfoById(id);
        return Response.success("成功", student);
    }

    @PutMapping("/updateStudentInfo")
    public Response updateStudentInfo(@RequestBody Student student) {
        int row = studentMapper.updateStudentInfo(student);
        return Response.success("成功", row);
    }

    @PostMapping("/createStudent")
    public Response createStudent(@RequestBody Student student) {
        int row = studentMapper.createStudent(student);
        return Response.success("成功", row);
    }

    @DeleteMapping("/{id}")
    public Response deleteStudent(@PathVariable("id") int id) {
        int row = studentMapper.deleteStudent(id);
        return Response.success("删除成功", row);
    }

    @GetMapping("/list")
    public Response list(Student student, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        // 分页
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = studentMapper.selectAll(student);
        return Response.page(students, new PageInfo<>(students).getTotal());
    }
}
